<template>
    <div>

        <van-form @submit="onSubmit">
            <van-field
                    v-model="username"
                    name="用户名"
                    label="用户名"
                    placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                    v-model="password"
                    type="password"
                    name="密码"
                    label="密码"
                    placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
            />
            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit" color="linear-gradient(to right, #84fab0, #8fd3f4)" >
                    提交
                </van-button>
            </div>
        </van-form>
        <div style="margin: 16px;">
            <van-button round block type="info"  color="linear-gradient(to right, #f6d365, #fda085)" @click="onRegister">
                注册
            </van-button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                username: '',
                password: '',
            };
        },
        methods: {
            onClickLeft(){
                this.$router.back();
            },
            onSubmit(values) {
                // this.$store.state.isLogin = true;
                // this.$store.state.userId = '';
                this.$axios.post('/findBuyer',{
                    username: this.username,
                    password: this.password,
                }).then(res=>{
                    if(res.data.code == "200"){
                        this.$store.state.isLogin = true;
                        this.setCookie("login","true",15);
                        this.$router.replace('/host');
                    } else {
                        console.log(res.data.message);
                    }
                })

            },
            onRegister(){
                this.$router.push("/register")
            },
            //创建一个Cookie
            setCookie(c_name,value,expiredays){
                let exdate = new Date();
                exdate.setDate(exdate.getDate()+expiredays);
                document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "":";expires=" + exdate.toGMT)
            }
        },
    }
</script>

<style scoped>

</style>